﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
using System.Runtime.Serialization.Json;
using Outlook2010Translate;
using System.Web;

namespace GoogleTranslate
{
    public class GoogleTranslator : Outlook2010Translate.ITranslate
    {       

        public GoogleTranslator()
        {

        }
        
        public string FriendlyName(string friendlyName)
        {
            return "Google Translator";
        }

        public string Translate(string textToTranslate, Outlook2010Translate.IAddValue cultureInfo)
        {
            string contentToTranslate = textToTranslate.Trim();
            string translatedText = String.Empty;
            
            string sLocale = cultureInfo.Value;

            if (String.IsNullOrEmpty(contentToTranslate))
            {
                return String.Empty;
            }
            ArrayList alToTranslate = new ArrayList();
            StringBuilder sbAggregatedTranslation = new StringBuilder();
            StringBuilder sb = new StringBuilder();
            int x = 0;
            foreach (char c in contentToTranslate)
            {
                if (x == 400)
                {
                    alToTranslate.Add(sb.ToString());
                    sb.Clear();
                    x = 0;
                }
                else
                {
                    sb.Append(c);
                    x++;
                }
            }

            if (sb != null)
            {
                alToTranslate.Add(sb.ToString());
            }


            foreach (string item in alToTranslate)
            {
                WebResponse resp = null;
                try
                {
                    string sKey = String.Empty;
                    //if (!String.IsNullOrEmpty(sGoogleTranslateAPIKey))
                    //{
                    //    sKey = "&key=" + sGoogleTranslateAPIKey;
                    //}
                    Uri url = new Uri("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&format=text&q=" + HttpUtility.UrlEncode(item) + sKey + "&userip=" + Dns.GetHostName() + "&langpair=%7C" + sLocale);
                    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
                    //myReq.Referer = "Outlook2010Translate";
                    resp = myReq.GetResponse();
                    StreamReader readStream = new StreamReader(resp.GetResponseStream(), Encoding.UTF8);
                    string JsonObject = readStream.ReadLine();
                    using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(JsonObject)))
                    {
                        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(TranslateResponse));
                        TranslateResponse translation = ser.ReadObject(ms) as TranslateResponse;
                        switch (translation.responseStatus)
                        {
                            case "200":
                                sbAggregatedTranslation.Append(HttpUtility.UrlDecode(translation.responseData.translatedText));
                                break;
                            case "400":
                                sbAggregatedTranslation.Append("Not currently supported language - Sorry. " + translation.responseDetails);
                                break;
                            default:
                                sbAggregatedTranslation.Append(translation.responseDetails);
                                break;
                        }

                    }
                }
                finally
                {
                    resp.Close();
                }

            }

            return sbAggregatedTranslation.ToString();
        }

        
    }

   
}
